﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace CloudSharingStation_UWP_
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class RegisterPage : Page
    {
        public RegisterPage()
        {
            this.InitializeComponent();
        }

        private void btnRegisterClick(object sender, RoutedEventArgs e)
        {
            if (cmbDepartment.SelectedItem==null||txtUserName.Text==null||txtPassword.Password==null||txtRepeatPwd.Password==null)
            {
                ContentDialog dialog = new ContentDialog();
                dialog.Title = "注册失败";
                dialog.Content = "请填写所有需要的信息！";
                dialog.CloseButtonText = "关闭";
                var result = dialog.ShowAsync();
                return;
            }
            string userName = txtUserName.Text;
            string password = txtPassword.Password;
            string repeatPassword = txtRepeatPwd.Password;
            string departmentName = (string)cmbDepartment.SelectedItem;
            Model.User.SetUser(userName,password,departmentName); // 声明注册用户对象，单例
            Dictionary<HttpStatusCode,string> res;
            res = Model.User.user.RegisterAsync(); // 发送注册Http请求
            if (res.First().Key != HttpStatusCode.OK)
            {
                ContentDialog dialog = new ContentDialog();
                dialog.Title = "注册失败";
                if (res.First().Value == "用户名已存在")
                {
                    dialog.Content = "注册失败！用户名已存在";
                }
                dialog.CloseButtonText = "关闭";
                var result = dialog.ShowAsync();
            }
            else
            {
                Frame root = Window.Current.Content as Frame;
                root.Navigate(typeof(LoginPage));
            }

        }
    }
}
